$(function () {
    var list;
    var shopcart = get();
    $.ajax("./javascripts/data.json", {
        dataType: "json",
    }).then(function (res) {
        list = res.data;
        var html = list.map(function (item, index) {
            return `<div class="feed_item">
        <div class="feed_img">
        <img class="lazyload" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2228985605,2720940451&fm=26&gp=0.jpg"
          data-src="${item.path}" 
          alt=""
        />
        </div>
        <p class="name">${item.title}</p>
        <p class="item_money">${item.price}</p>
        <p class="label_line">
        <a href="javascript:;" class="label" data-index=${index} "">加入购物车</a>
      </p>
    </div>`;
        }).join("");
        $(".feed_list").html(html);
        lazyload()

        // 事件委托
        $(".feed_list").on("click", ".label", function () {
            var index = $(this).attr("data-index")
            if (shopcart[index]) {
                shpcart[index].count++;
            } else {
                list[index].count = 1;
                shopcart[index] = list[index]
            }
            save(shopcart)
            num();
            // location.reload()
        })
    })

    // 购物车数量
    function num() {
        var data = localStorage.getItem("shopcart")
        data = JSON.parse(data)
        var arr = Object.keys(data)
        var counts = arr.length
        $(".car em").html(counts)
    }
    num()
})